package com.atguigu.wirte;

import com.atguigu.bean.Movie;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.Index;

import java.io.IOException;

public class Single_Write {
    public static void main(String[] args) throws IOException {
        //1.创建客户端工厂
        JestClientFactory jestClientFactory = new JestClientFactory();

        //设置ES连接地址
        HttpClientConfig httpClientConfig = new HttpClientConfig.Builder("http://hadoop102:9200").build();

        jestClientFactory.setHttpClientConfig(httpClientConfig);

        //2.获取连接
        JestClient jestClient = jestClientFactory.getObject();

        //3.使用连接，往ES中写入数据
        Movie movie = new Movie("103", "泰坦尼克号");
        Index index = new Index.Builder(movie)
                .index("movie1")
                .type("_doc")
                .id("1003")
                .build();
      /*  Index index = new Index.Builder("{\n" +
                "  \"id\":\"102\",\n" +
                "  \"name\":\"太极张三丰\"\n" +
                "}")
                .index("movie1")
                .type("_doc")
                .id("1002")
                .build();*/

        jestClient.execute(index);

        //关闭连接
        jestClient.shutdownClient();
    }
}
